<?php
namespace app\web\model;

use app\lib\model\Price;
/**
 * @package app\basic\model
 */
class TechnicianBalance extends Base 
{
    public function getCreateTimeAttr($v)
    {
        return date('Y-m-d',$v);
    }
    public function getUpdateTimeAttr($v)
    {
        return date('Y-m-d',$v);
    }
    /**
     * [change description]
     * @param  [type]  $user_id        [description]
     * @param  [type]  $change_balance [description]
     * @param  [type]  $balance_type   [description]
     * @param  string  $notes          [description]
     * @param  integer $order_id       [description]
     * @return [type]                  [description]
     */
    public static function change($t_id,$change_balance,$balance_type,$notes = '',$order_id = 0,$status = 1)
    {
        $user = Technician::get($t_id);
        $after_balance = Price::pricecalc($user->balance,'+',$change_balance);
        $data = [
            't_id' => $t_id,
            'balance_type' => $balance_type,
            'order_id' => $order_id,
            'before_balance' => $user->balance,
            'change_balance' => $change_balance,
            'after_balance' => $after_balance,
            'status' => $status,
            'notes' => $notes,
            'appkey' => 1,
        ];
        $res = self::create($data);
        if($res->isEmpty()) return false;
        $user->balance = $after_balance;
        if($status == 1){
            $res = $user->save();
        }
        return $res ? true : false;
    } 
}